﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMS.Global;
using SIMS.Database;
using System.Collections;
using System.Data;

namespace SIMS.School
{
    public class Exam
    {
        public int Id { get; set; }
        public int CompanyId { get; set; }
        public string CompanyName { get; set; }

        public string Name { get; set; }
        public string Description { get; set; }

        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

        public MyError Error { get; set; }
    }

    public class ExamList : List<Exam>
    {
        public MyError Error { get; set; }
        public ExamList()
        {
        }
    }

    public class ExamDB
    {
        MyDatabase _db = new MyDatabase();

        public Exam Save(Exam Exam)
        {
            try
            {
                SortedList _param = new SortedList();
                _param.Add("@Id", Exam.Id);
                _param.Add("@CompanyId", Exam.CompanyId);

                if (Exam.Description != string.Empty)
                {
                    _param.Add("@Description", Exam.Description);
                }
                else
                {
                    _param.Add("@Description", DBNull.Value);
                }

                _param.Add("@Name", Exam.Name);


                _param.Add("@IsActive", Exam.IsActive);

                int _affRow = _db.ExecuteNonSqlSP("usp_ExamInsertUpdateSingleItem", _param);

                if (_affRow > 0)
                {
                    Exam.Error.ErrorNo = 0;
                }
                else
                {
                    Exam.Error.ErrorNo = 1;
                    Exam.Error.ErrorMessage = "No row affect when saving school's Exam details.";
                }
            }
            catch (Exception Ex)
            {
                Exam.Error.ErrorNo = 10002;
                Exam.Error.ErrorLocation = "Exam";
                Exam.Error.ErrorMessage = Ex.Message;
                Exam.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Exam");
            }

            return Exam;
        }

        public ExamList GetExamList(int CompanyId)
        {
            ExamList retValue = new ExamList();

            try
            {
                DataTable _dt = new DataTable("tblExam");

                SortedList _param = new SortedList();
                _param.Add("@CompanyId", CompanyId);
                _dt = _db.GetDataTableBySP("usp_ExamSelectItemList", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        Exam _Exam = new Exam();
                        FillData(_dr, _Exam);
                        retValue.Add(_Exam);
                    }
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "Exam";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Exam");
            }

            return retValue;
        }

        public Exam GetExam(int Id, int CompanyId)
        {
            Exam retValue = new Exam();

            try
            {

                DataTable _dt = new DataTable("tblExam");

                SortedList _param = new SortedList();
                _param.Add("@Id", Id);
                _param.Add("@CompanyId", CompanyId);
                _dt = _db.GetDataTableBySP("usp_ExamSelectSingleItem", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    FillData(_dt.Rows[0], retValue);
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "Exam";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Exam");
            }

            return retValue;
        }

        private void FillData(DataRow aRow, Exam Exam)
        {
            Exam.Id = Convert.ToInt32(aRow["Id"].ToString());
            Exam.CompanyId = Convert.ToInt32(aRow["CompanyId"].ToString());
            
            Exam.Error = new MyError();

            if (aRow["Description"] != DBNull.Value)
            {
                Exam.Description = aRow["Description"].ToString();
            }
            else
            {
                Exam.Description = string.Empty;
            }

            Exam.Name = aRow["Name"].ToString();
            Exam.CompanyName = aRow["CompanyName"].ToString();

            Exam.IsActive = Convert.ToBoolean(aRow["IsActive"].ToString());

            Exam.Error.ErrorNo = 0;
        }
    }
}
